package com.awesome.spring.mqtt.config;


import org.eclipse.paho.mqttv5.client.MqttClient;
import org.eclipse.paho.mqttv5.client.MqttConnectionOptions;
import org.eclipse.paho.mqttv5.common.MqttException;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @program: awesome-spring
 * @description: 配置类
 * @author: Alex Wu
 * @createDate: 2025-03-19 10:22
 **/
@Configuration
public class MqttClientConfig {

    private static final String MQTT_BROKER = "tcp://192.168.12.101:1883"; // EMQX 服务器地址
    private static final String CLIENT_ID = "springboot-mqtt-client";
    private static final String USERNAME = "admin";  // EMQX 用户名
    private static final String PASSWORD = "public"; // EMQX 密码

    @Bean
    public MqttClient mqttClient() throws MqttException {
        MqttClient client = new MqttClient(MQTT_BROKER, CLIENT_ID);
        MqttConnectionOptions options = new MqttConnectionOptions();
//        options.setUserName(USERNAME);
//        options.setPassword(PASSWORD.getBytes());
        options.setCleanStart(true);
        options.setAutomaticReconnect(true);
        client.connect(options);
        return client;
    }
}
